package com.licode.codekit.buildprj;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.util.ResourceUtil;
import com.licode.codekit.utils.FileUtil;
import com.licode.codekit.utils.PomFile;
import com.licode.codekit.utils.StrUtil;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
 * @author licode
 */
public class ModuleArchBuilder {

    public ModuleArchBuilder() {
    }

    private String getRootDir(String dir) {
        String zipRoot = dir;
        if (dir != null && dir.length() >= 2) {
            if (!dir.endsWith("/")) {
                zipRoot = dir + "/";
            }

            return zipRoot;
        } else {
            return "";
        }
    }

    private InputStream getTemplateFile(String moduleType) {
        return FileUtil.getResourceAsStream("normal-arch-dir");
    }

    public void buildModuleArch(ProjectContext prjCtx, String srcRoot) throws IOException {
        String moduleType = prjCtx.getConfig().getModuleType();

        try (InputStream tempIs = getTemplateFile(moduleType);
             ZipInputStream zipIs = new ZipInputStream(tempIs, StandardCharsets.UTF_8)) {
            for (ZipEntry nextEntry = zipIs.getNextEntry(); nextEntry != null; nextEntry = zipIs.getNextEntry()) {
                String name = nextEntry.getName();
                String currDir = srcRoot + "/" + name;
                File currFile = new File(currDir);
                if (nextEntry.isDirectory()) {
                    FileUtil.makeDir(currFile);
                } else {
                    FileUtil.saveText(zipIs, currDir);
                }
                zipIs.closeEntry();
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

}
